package redteam.grabthegreen;

import android.os.Bundle;
import android.view.View;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayController;
import com.google.android.maps.Point;

public class SetBoundsWindow extends MapActivity {
	private boolean onFirstClick;
	private Point p1;
	private Point p2;
	private MapIcon mi1;
	private MapIcon mi2;
	private MapField mf;
	private OverlayController oc;
	private MapController mc;
	private MapView mv;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		//MapView mv = (MapView)findViewById(R.id.mapview);
		onFirstClick = true;
		mv = new MapView(this);
		Point p = new Point((int) (34.020119 * 1000000),(int) (-118.283186 * 1000000));
		mc = mv.getController();
		oc = mv.createOverlayController();
		mc.animateTo(p);
		mc.zoomTo(21);
		setContentView(mv);
		//setContentView(R.layout.setboundswindow);
		mv.toggleSatellite();


		MapIcon m = new MapIcon(p);
		oc.add(m, false);
		oc.activate(m, false);

		mv.setOnLongPressListener(new MapView.OnLongPressListener() {
        	public boolean onLongPress(View v, float x, float y) {
        		if (onFirstClick)
        		{
        			p1 = new Point((int)x, (int)y);
        			onFirstClick = false;
        			if (mi1 != null)
        				oc.deactivate(mi1);
        			if (mf != null)
    					oc.deactivate(mf);
        			mi1 = new MapIcon(p1);
        			oc.add(mi1, false);
        			oc.activate(mi1, false);
        			if (mi2 != null)
        			{
        				if (p1.getLatitudeE6() < p2.getLatitudeE6())
            				mf = new MapField(p1,p2);
            			else
            				mf = new MapField(p2,p1);

            			oc.add(mf, false);
            			oc.activate(mf, false);
        			}
        			((MapView)v).invalidate();
        			//mc.animateTo(p1);
        		}
        		else
        		{
        			p2 = new Point((int)x, (int)y);
        			//mv.getM
        			onFirstClick = true;
        			if (mi2 != null)
        				oc.deactivate(mi2);
        			if (mf != null)
    					oc.deactivate(mf);
        			mi2 = new MapIcon(p2);
        			oc.add(mi2, false);
        			oc.activate(mi2, false);
        			if (p1.getLatitudeE6() < p2.getLatitudeE6())
        				mf = new MapField(p1,p2);
        			else
        				mf = new MapField(p2,p1);
        			oc.add(mf, false);
        			oc.activate(mf, false);
        			((MapView)v).invalidate();
        			//mc.animateTo(p2);
        		}
        		return true;
        	}
        });

		/*
		Button okButton = (Button)findViewById(R.id.okbutton);
		okButton.setText("Select Game Area");
		okButton.setOnClickListener(new Button.OnClickListener() {
	    	public void onClick(View v) {
	    		//return bounds info and go back
	    	}
	    });
	    */
	}
}
